/**
 * 122. 买卖股票的最佳时机 II
 * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/
 * 进阶：动态规划
 */
public class Solutions_122 {
    public static void main(String[] args) {
//        int[] nums = {7, 1, 5, 3, 6, 4};  // output: 7
//        int[] nums = {1, 2, 3, 4, 5};  // output: 4
        int[] nums = {7, 6, 4, 3, 1};  // output: 0

        int result = maxProfit(nums);
        System.out.println(result);
    }

    /**
     * 题目化简为：当天卖出后，再以当天的价格买入
     * 后续只要是当天的价格比买入的价格高，即再卖出，再买入
     */
    public static int maxProfit(int[] prices) {
        int res = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1]) {
                res += prices[i] - prices[i - 1];
            }
        }
        return res;
    }
}
